﻿using StudentControlSystem.Entity;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentControlSystem.Controller
{
    internal class ClassController
    {
        string connectionString = DBHelper.ConnStrings;

        public void AddClass(Class classInfo)
        {
            using (var connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                var command = new SQLiteCommand("INSERT INTO Classes (ClassName, SchoolId) VALUES (@name, @schoolId)", connection);
                command.Parameters.AddWithValue("@name", classInfo.ClassName);
                command.Parameters.AddWithValue("@schoolId", classInfo.SchoolId);
                command.ExecuteNonQuery();
                Log.LogAction("Added class: " + classInfo.ClassName);
            }
        }

        public void UpdateClass(Class classInfo)
        {
            using (var connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                var command = new SQLiteCommand("UPDATE Classes SET ClassName = @name WHERE ClassId = @id", connection);
                command.Parameters.AddWithValue("@name", classInfo.ClassName);
                command.Parameters.AddWithValue("@id", classInfo.ClassId);
                command.ExecuteNonQuery();
                Log.LogAction("Updated class: " + classInfo.ClassName);
            }
        }

        public void DeleteClass(int classId)
        {
            using (var connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                var command = new SQLiteCommand("DELETE FROM Classes WHERE ClassId = @id", connection);
                command.Parameters.AddWithValue("@id", classId);
                command.ExecuteNonQuery();
                Log.LogAction("Deleted class with ID: " + classId);
            }
        }

        public List<Class> GetClassesBySchoolId(int schoolId)
        {
            var classes = new List<Class>();
            using (var connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                var command = new SQLiteCommand("SELECT * FROM Classes WHERE SchoolId = @schoolId", connection);
                command.Parameters.AddWithValue("@schoolId", schoolId);
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        classes.Add(new Class
                        {
                            ClassId = reader.GetInt32(0),
                            ClassName = reader.GetString(1),
                            SchoolId = reader.GetInt32(2)
                        });
                    }
                }
            }
            return classes;
        }

        public Class GetById(int classId)
        {
            Class classInfo = null;
            using (var connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                var command = new SQLiteCommand("SELECT * FROM Classes WHERE ClassId = @id", connection);
                command.Parameters.AddWithValue("@id", classId);
                using (var reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        classInfo = new Class
                        {
                            ClassId = reader.GetInt32(0),
                            ClassName = reader.GetString(1),
                            SchoolId = reader.GetInt32(2)
                        };
                    }
                }
            }
            return classInfo;
        }
    }
}

